import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // TRY THIS: Try running your application with "flutter run". You'll see
        // the application has a purple toolbar. Then, without quitting the app,
        // try changing the seedColor in the colorScheme below to Colors.green
        // and then invoke "hot reload" (save your changes or press the "hot
        // reload" button in a Flutter-supported IDE, or press "r" if you used
        // the command line to start the app).
        //
        // Notice that the counter didn't reset back to zero; the application
        // state is not lost during the reload. To reset the state, use hot
        // restart instead.
        //
        // This works for code too, not just values: Most code changes can be
        // tested with just a hot reload.
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    TextStyle style = const TextStyle(
      fontSize: 18,
      fontWeight: FontWeight.normal,
    );

    String text = "qweqqe11";
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          children: [
            Container(
              child: Text(
                text,
                style: style,
                locale: WidgetsBinding.instance.window.locale,
                textScaleFactor: MediaQuery.of(context).textScaleFactor,
              ),
              color: Colors.red,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          RenderParagraph aaa = RenderParagraph(
            TextSpan(children: [
              TextSpan(text: text, style: style),
            ]),
            textAlign: TextAlign.center,
            textDirection: TextDirection.ltr,
            softWrap: true,
            overflow: TextOverflow.clip,
            textScaleFactor: MediaQuery.of(context).textScaleFactor,
            locale: WidgetsBinding.instance.window.locale,
            textWidthBasis: TextWidthBasis.parent,
            maxLines: null,
          );

          Size sizeaa = ChildLayoutHelper.dryLayoutChild(
              aaa,
              BoxConstraints(
                maxWidth: 400,
                minHeight: 10,
                minWidth: 10,
                maxHeight: 300,
              ));

          print("sizeaaa====${sizeaa.toString()}");
          Size b = computeTextSize(context, 400, text, style,
              fixWidth: 0, maxLines: 100);
          print("b===$b");
          return;

          RenderParagraph a = RenderParagraph(
            TextSpan(text: text, style: style),
            textAlign: TextAlign.center,
            textDirection: TextDirection.ltr,
            softWrap: true,
            overflow: TextOverflow.clip,
            textScaleFactor: 1,
            textWidthBasis: TextWidthBasis.parent,
            maxLines: null,
          );

          ///这种计算不了有WidgetSpan的富文本
          a.layout(BoxConstraints(
              maxWidth: 400, maxHeight: 500, minHeight: 0, minWidth: 0));

          print("a.size===${a.size}");
        },
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  Size computeTextSize(
      context, double maxWidth, String value, TextStyle textStyle,
      {int maxLines = 1, double fixWidth = 2.0, double fixHeight = 0.0}) {
    //很迷的bug，这行代码不要动
    value = value.replaceAll('/', '*').replaceAll('-', ',');

    TextPainter painter = TextPainter(
      ///AUTO：华为手机如果不指定locale的时候，该方法算出来的文字高度是比系统计算偏小的。
      // ignore: deprecated_member_use
      locale: WidgetsBinding.instance.window.locale,
      maxLines: maxLines,
      textDirection: TextDirection.ltr,
      //字体缩放大小
      textScaleFactor: MediaQuery.of(context).textScaleFactor,
      text: TextSpan(text: value, style: textStyle),
    );
    painter.layout(maxWidth: maxWidth);

    ///文字的宽度:painter.height
    return Size(painter.width + fixWidth, painter.height + fixHeight);
  }
}
